package fsoft.com.vn.directory.dao;

import fsoft.com.vn.directory.form.Business;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

@Repository
public class BusinessDAOImpl implements BusinessDAO {

	  @Autowired
      private SessionFactory sessionFactory;
      private Session session;

	@SuppressWarnings("unchecked")
	public List<Business> listBusiness(int page) {
		Query q = sessionFactory.getCurrentSession().createQuery("from Business");
		q.setMaxResults(4);
		if(page > 0){
			q.setFirstResult(((page-1)*4));
		}
		return q.list();
	}


	public List<Business> search(String businessname, String siccode) {
		session = sessionFactory.openSession();
        Criteria cr = session.createCriteria(Business.class);
        cr.add(Restrictions.like("businessname","%"+businessname+"%"));
        cr.add(Restrictions.like("sic","%"+siccode+"%"));
        return cr.list();   

	}

}
